package com.example.easybuy.controller;

import com.example.easybuy.entity.Car;
import com.example.easybuy.entity.CarDetail;
import com.example.easybuy.entity.OrderDetail;
import com.example.easybuy.service.CarService;
import com.example.easybuy.util.AjaxResult;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("car")
public class CarController {
    @Autowired
    private CarService carService;

    @RequestMapping("/getCar")
    public AjaxResult getCar(String loginName) {//接受用户名
        Car car = carService.getCar(loginName);
        return AjaxResult.success().add("car", car).add("carMap", car.getProductMap());
    }

    @RequestMapping("/addCar")
    public AjaxResult addCar(@RequestParam Map<String, Object> map) {
        return AjaxResult.success().add("isAdd", carService.addCar(map));
    }

    @PostMapping("/updateCar")
    public AjaxResult updateCar(@RequestBody Map<String, String> map) {
        String listJson=map.get("updateCarList");
        ObjectMapper objectMapper=new ObjectMapper();
        List<CarDetail> list=new ArrayList<>();
        try {
            list=objectMapper.readValue(listJson, new TypeReference<ArrayList<CarDetail>>() {});
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return carService.updateCar(list,map.get("loginName"));
    }

    @RequestMapping("/delCar")
    public AjaxResult delCar(Integer productId,String loginName) {
        return carService.delCar(loginName,productId);
    }
}
